package de.nak.fireDepartment.administrationAction;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.struts.ActionSupport;

import de.nak.fireDepartment.actionForms.LoginForm;
import de.nak.fireDepartment.model.User;
import de.nak.fireDepartment.service.LoginService;

/**
 * Action that validates the parameters in the login-process
 * 
 * @author Sven Redelin
 */
public class LoginValidateAction extends ActionSupport {

	/**
	 * This method validates the input-parameters for the login-process
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return actionforward
	 * @throws Exception
	 */
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		// ActionForm => LoginForm
		LoginForm loginForm = (LoginForm) form;
		
		WebApplicationContext ctx = getWebApplicationContext();
		LoginService loginService = (LoginService)ctx.getBean("loginService");
		
		// create new UserObject with inputparameters
		User newUser = new User();
		newUser.setPassword(loginForm.getPassword());
		newUser.setUserName(loginForm.getUserName());
		
		// if user is validated return success, else return fail and display wrong Data.
		if(loginService.validateLogin(newUser)){
			loginForm.setWrongData("");
			return mapping.findForward("success");
		}else{
			loginForm.setWrongData("Login incorect!");
			return mapping.findForward("fail");
		}
	}
}
